import { _decorator, CCInteger, Color, Component, Node, ParticleSystem, Tween, tween, Vec3 } from 'cc';
import { PoolManager } from '../../Scripts/Framework/Managers/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('Bullet')
export class Bullet extends Component {

    @property(ParticleSystem)
    particle: ParticleSystem;

    @property(CCInteger)
    speed = 1;

    Init(startPos: Vec3, endPos: Vec3, call = () => { }) {
        this.particle.stop();
        this.particle.play();
        for (let i = 0; i < this.node.children.length; i++) {
            const element = this.node.children[i].getComponent(ParticleSystem);
            if (element) {
                element.stop();
                element.play();
            }
        }
        this.node.setWorldPosition(startPos);
        this.node.active = true;
        let dis = Vec3.distance(endPos, startPos);
        let time = dis / this.speed;
        // this.particle.trailModule.lifeTime.constant = time;
        Tween.stopAllByTarget(this.node);
        tween(this.node)
            .to(time, { worldPosition: endPos })
            .delay(2)
            .call(() => {
                call();
                PoolManager.PutNode(this.node);
            })
            .start();
    }

}


